﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TQB.DEMO.Castle.Entities;

using Castle.ActiveRecord;

namespace TQB.DEMO.Castle.WebApp
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TestCascadingSave();

        }

        /// <summary>
        /// 执行事务，持久化对象到数据库
        /// </summary>
        public void TestCascadingSave()
        {
            //创建Blog对象
            TQBBlog blog = new TQBBlog();
            blog.BlogName = "my programe teacher";
            blog.BlogAuthor = "dddddddddddddddd";

            //执行事务，持久化对象到数据库

            using (TransactionScope tran = new TransactionScope())
            {
                //using (new SessionScope()) ;
                try
                {
                    blog.Create();
                    for (int i = 0; i < 11; i++)
                    {
                        TQBPost post = new TQBPost();
                        post.PostTitle = "this is my " + i + "artices";
                        post.PostCategories = "Castle Learn";
                        post.PostCreated = DateTime.Now;  //要给时间赋值，否则会出现Could not perform Save for Post的错误
                        post.Blog = blog;
                        post.Save();  //这个地方将文章保存到session中
                    }
                    tran.VoteCommit();  //统一提交到数据库中
                }
                catch
                {
                    tran.VoteRollBack();
                }
            }
        }

    }
}